<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item
        v-for="(item,index) in levelList"
        :key="index"
        v-if="item"
        style="width:auto"
      >
        <span
          v-if="
            item.redirect === 'noredirect' || index === levelList.length - 1
          "
          class="no-redirect"
        >{{ item.meta.title }}</span>
        <router-link v-else :to="item.redirect || item.path">
          {{
          item.meta.title
          }}
        </router-link>
      </el-breadcrumb-item>
    </el-breadcrumb>
  </div>
</template>

<script>
export default {
  name: "Breadcrumb",
  components: {},
  data() {
    return {
      levelList: null
    };
  },
  watch: {
    // 路由监听：监听路由的变化，从而做出相应操作
    $route: {
      immediate: true, // 一旦监听到路由的变化立即执行
      handler(to, from) {
        let path = to.path;
        localStorage.setItem("path", path);
      }
    }
  },
  created() {
    this.getBreadcrumb();
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.name);
      console.log(matched);

      const first = matched[0];
      if (first && first.name !== "container") {
        matched = [{ path: "/container", meta: { title: "面板" } }].concat(
          matched
        );
      }
      this.levelList = matched;
      console.log(matched);
    }
  }
};
</script>
<style lang="scss" scoped>
// .no-redirect {
//   color: #000;
//   cursor: text;
// }
</style>